package com.kaifamiao.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.logging.Logger;

public class ApplicationLifecycleListener implements ServletContextListener {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    // 当 容器 完成对 当前应用 的 初始化 后 调用该方法
    @Override
    public void contextInitialized( ServletContextEvent event ) {
        ServletContext application = event.getServletContext();
        String contextPath = application.getContextPath();
        String message = contextPath == null || contextPath.isEmpty() ? "" : "[ " + contextPath + " ]";
        logger.info( "已经启动" + message + "应用" );
    }

    // 当 容器 完成对 当前应用 的 卸载 后 调用该方法
    @Override
    public void contextDestroyed( ServletContextEvent event ) {
        ServletContext application = event.getServletContext();
        String contextPath = application.getContextPath();
        String message = contextPath == null || contextPath.isEmpty() ? "" : "[ " + contextPath + " ]";
        logger.info( "已经停止" + message + "应用" );
    }

}
